package com.hb.trade.server.center.au.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.au.dto.OrgCollectionDTO;
import com.hb.core.pojo.center.au.dto.OrgPayeeDTO;
import com.hb.core.pojo.center.au.entity.OrgCollection;
import com.hb.core.pojo.center.au.entity.OrgPayee;
import com.hb.core.pojo.enums.PayTypeEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.center.au.manager.OrgPayeeManger;
import com.hb.trade.server.center.au.service.OrgCollectionService;
import com.hb.trade.server.center.au.service.OrgPayeeService;

import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 企业收款方表(子表au_org_collection) 前端控制器
 * </p>
 *
 * @author zhongdl
 * @since 2022-02-14
 */
@RestController
public class OrgPayeeController {

    protected final OrgPayeeService orgPayeeService;
    protected final OrgCollectionService orgCollectionService;

    public OrgPayeeController(OrgPayeeService orgPayeeService,
                              OrgCollectionService orgCollectionService) {
        this.orgPayeeService = orgPayeeService;
        this.orgCollectionService = orgCollectionService;
    }

    /**
     * @param orgPayeeDTO:
     * @Description: 新增一个收款方
     * @Author: zhongdl
     * @Date:2022/2/14 9:24
     * @return: {@link Message < Boolean> }
     **/
    @PostMapping("/orgPayee/newOrgPayee")
    @Transactional
    public Message<Long> newOrgPayee(@RequestBody OrgPayeeDTO orgPayeeDTO) {
        OrgPayeeManger orgPayeeManger = orgPayeeDTO.to(OrgPayeeManger.class);
        orgPayeeManger.fillOrgPayee();
        boolean save = orgPayeeService.save(orgPayeeManger);
        boolean saveOrgCollection = orgCollectionService.saveOrgCollection(orgPayeeManger.getOrgCollectionList());
        if (save && saveOrgCollection){
            return MsgUtil.success(orgPayeeManger.getId());
        }
        return MsgUtil.fail("新增失败");
    }

    /**
     * @param orgPayeeDTO:
     * @Description: 更新收款方信息
     * @Author: zhongdl
     * @Date:2022/2/14 10:31
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/orgPayee/updateOrgPayee")
    public Message<Boolean> updateOrgPayee(@RequestBody OrgPayeeDTO orgPayeeDTO) {
        OrgPayeeManger orgPayeeManger = orgPayeeDTO.to(OrgPayeeManger.class);
        orgPayeeManger.fillOrgPayee();
        boolean update = orgPayeeService.updateById(orgPayeeManger);
        boolean removeByPayeeId = orgCollectionService.removeByPayeeId(orgPayeeManger.getId());
        boolean saveOrgCollection = orgCollectionService.saveOrgCollection(orgPayeeManger.getOrgCollectionList());
        return MsgUtil.success(update && saveOrgCollection);
    }

    /**
     * @param orgPayeeDTO:
     * @Description: 查询收款方列表接口
     * @Author: zhongdl
     * @Date:2022/2/14 10:37
     * @return: {@link Message< List<  OrgCollection >> }
     **/
    @PostMapping("/orgPayee/listOrgPayee")
    public Message<List<OrgPayeeDTO>> listOrgPayee(@RequestBody OrgPayeeDTO orgPayeeDTO) {
        List<OrgPayeeDTO> payeeDTOList = orgPayeeService.listOrgPayee(orgPayeeDTO.getOrgId());
        if (CollUtil.isEmpty(payeeDTOList)) {
            return MsgUtil.success(payeeDTOList);
        }
        //查询收款方式
        for (OrgPayeeDTO payeeDTO : payeeDTOList) {
            //查询支付宝
            List<OrgCollectionDTO> alipayInfo = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.ALI_PAY.getCode());
            if (CollUtil.isNotEmpty(alipayInfo)) {
                payeeDTO.setAlipayInfo(alipayInfo);
            }
            //微信
            List<OrgCollectionDTO> weChatInfo = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.WE_CHAT_PAY.getCode());
            if (CollUtil.isNotEmpty(weChatInfo)) {
                payeeDTO.setWeChatInfo(weChatInfo);
            }
            //银联
            List<OrgCollectionDTO> unionPayInfos = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.UNION_PAY.getCode());
            if (CollUtil.isNotEmpty(unionPayInfos)) {
                OrgCollectionDTO orgCollectionDTO = unionPayInfos.get(0);
                if (orgCollectionDTO.validBankInfo() == null) {
                    payeeDTO.setUnionPayInfo(CollUtil.isNotEmpty(unionPayInfos) ? orgCollectionDTO : null);
                }
            }
        }
        return MsgUtil.success(payeeDTOList);
    }

    /**
     * @param orgPayeeDTO:
     * @Description: 查询收款方列表接口(分页)
     * @Author: zhongdl
     * @Date:2022/2/14 10:37
     * @return: {@link Message< List<  OrgCollection >> }
     **/
    @PostMapping("/orgPayee/pageOrgPayee")
    public Message<PageResult<OrgPayeeDTO>> pageOrgPayee(@RequestBody OrgPayeeDTO orgPayeeDTO) {
        Page<OrgPayeeDTO> page = orgPayeeService.pageOrgPayee(orgPayeeDTO);
        List<OrgPayeeDTO> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return MsgUtil.success(PageResult.get(page));
        }
        //查询收款方式
        for (OrgPayeeDTO payeeDTO : records) {
            //查询支付宝
            List<OrgCollectionDTO> alipayInfo = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.ALI_PAY.getCode());
            if (CollUtil.isNotEmpty(alipayInfo)) {
                payeeDTO.setAlipayInfo(alipayInfo);
            }
            //微信
            List<OrgCollectionDTO> weChatInfo = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.WE_CHAT_PAY.getCode());
            if (CollUtil.isNotEmpty(weChatInfo)) {
                payeeDTO.setWeChatInfo(weChatInfo);
            }
            //银联
            List<OrgCollectionDTO> unionPayInfos = orgCollectionService.listOrgCollectionByPayeeId(payeeDTO.getId(), PayTypeEnum.UNION_PAY.getCode());
            if (CollUtil.isNotEmpty(unionPayInfos)) {
                OrgCollectionDTO orgCollectionDTO = unionPayInfos.get(0);
                if (orgCollectionDTO.validBankInfo() == null) {
                    payeeDTO.setUnionPayInfo(CollUtil.isNotEmpty(unionPayInfos) ? orgCollectionDTO : null);
                }
            }
        }
        return MsgUtil.success(PageResult.get(page));
    }

    /**
     * @param orgPayee:
     * @Description: 查询收款方详情
     * @Author: zhongdl
     * @Date:2022/2/14 10:51
     * @return: {@link Message< OrgPayeeDTO> }
     **/
    @PostMapping("/orgPayee/getOrgPayeeId")
    public Message<OrgPayeeDTO> getOrgOrgPayeeId(@RequestBody OrgPayeeDTO orgPayee) {
        OrgPayee orgPayeeDB = orgPayeeService.getById(orgPayee.getId());
        if (orgPayeeDB == null) {
            return MsgUtil.success(orgPayee);
        }
        OrgPayeeDTO orgPayeeDTO = orgPayeeDB.to(OrgPayeeDTO.class);
        //查询收款方式
        List<OrgCollectionDTO> orgCollectionDTOS = orgCollectionService.listOrgCollectionByPayeeId(orgPayeeDTO.getId());
        orgPayeeDTO.setOrgCollectionList(orgCollectionDTOS);
        //查询支付宝
        List<OrgCollectionDTO> alipayInfo = orgCollectionService.listOrgCollectionByPayeeId(orgPayeeDTO.getId(), PayTypeEnum.ALI_PAY.getCode());
        orgPayeeDTO.setAlipayInfo(alipayInfo);
        //微信
        List<OrgCollectionDTO> weChatInfo = orgCollectionService.listOrgCollectionByPayeeId(orgPayeeDTO.getId(), PayTypeEnum.WE_CHAT_PAY.getCode());
        orgPayeeDTO.setWeChatInfo(weChatInfo);
        //银联
        List<OrgCollectionDTO> unionPayInfos = orgCollectionService.listOrgCollectionByPayeeId(orgPayeeDTO.getId(), PayTypeEnum.UNION_PAY.getCode());
        orgPayeeDTO.setUnionPayInfo(CollUtil.isNotEmpty(unionPayInfos) ? unionPayInfos.get(0) : null);
        return MsgUtil.success(orgPayeeDTO);
    }

    /**
     * @param orgPayeeDTO:
     * @Description: 根据id删除收款方
     * @Author: zhongdl
     * @Date:2022/2/14 10:59
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/orgPayee/removeOrgPayeeId")
    public Message<Boolean> removeOrgPayeeId(@RequestBody OrgPayeeDTO orgPayeeDTO) {
        //校验收款方式是否被关联
        List<OrgCollectionDTO> orgCollectionDTOS = orgCollectionService.listOrgCollectionByPayeeId(orgPayeeDTO.getId());
        if (CollUtil.isEmpty(orgCollectionDTOS)){
            orgCollectionDTOS = CollUtil.list(false);
        }
        for (OrgCollectionDTO orgCollectionDTO : orgCollectionDTOS) {
            String msg = orgCollectionService.correlatedFlag(orgCollectionDTO.getId());
            if (StrUtil.isNotEmpty(msg)){
                return MsgUtil.fail(msg);
            }
        }
        orgPayeeDTO.buildBaseData(3);
        boolean update = orgPayeeService.updateById(orgPayeeDTO);
        boolean updateCollection = orgCollectionService.removeByPayeeId(orgPayeeDTO.getId());
        return MsgUtil.success(update && updateCollection);
    }

    /**
     *
     * @Description:校验收款方式是否被关联
     * @Author: zhongdl
     * @Date:2022/5/30 11:43
     * @param orgCollectionDTO:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/orgCollection/correlatedFlag")
    public Message<Boolean> correlatedFlag(@RequestBody OrgCollectionDTO orgCollectionDTO) {
        if (orgCollectionDTO.getId() == null){
            return MsgUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        String msg = orgCollectionService.correlatedFlag(orgCollectionDTO.getId());
        if (StrUtil.isNotEmpty(msg)){
            return MsgUtil.fail(msg);
        }
        return MsgUtil.success(true);
    }
}
